function formatDate(date) {   //当前日期
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    return [year, month, day].map(formatNumber).join('-')
}

function formatDateBefore(date) {   //当前日期前十年
    var year = date.getFullYear() - 10
    var month = date.getMonth() + 1
    var day = date.getDate()
    return [year, month, day].map(formatNumber).join('-')
}

function formatDateAfter(date) {   //当前日期后十年
    var year = date.getFullYear() + 10
    var month = date.getMonth() + 1
    var day = date.getDate()
    return [year, month, day].map(formatNumber).join('-')
}

//计算天数差
function dateDifference(sDate1, sDate2) {    //sDate1和sDate2是2006-12-18格式  
    var dateSpan,
        tempDate,
        iDays;
    sDate1 = Date.parse(sDate1);
    sDate2 = Date.parse(sDate2);
    dateSpan = sDate2 - sDate1;
    dateSpan = Math.abs(dateSpan);
    iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
    return iDays
}

function formatTime(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}
//成功提示
function showSuccess(title = "成功啦", duration = 5000) {
    wx.showToast({
        title: title,
        icon: 'success',
        duration: (duration <= 0) ? 5000 : duration
    });
}
//loading提示
function showLoading(title = "请稍后", duration = 5000) {
    wx.showToast({
        title: title,
        icon: 'loading',
        duration: (duration <= 0) ? 5000 : duration
    });
}
//隐藏提示框
function hideToast() {
    wx.hideToast();
}

//显示带取消按钮的消息提示框
function alertViewWithCancel(title = "提示", content = "消息提示", confirm, showCancel = "true") {
    wx.showModal({
        title: title,
        content: content,
        showCancel: showCancel,
        success: function (res) {
            if (res.confirm) {
                confirm();
            }
        }
    });
}
//显示不取消按钮的消息提示框
function alertView(title = "提示", content = "消息提示", confirm) {
    alertViewWithCancel(title, content, confirm, false);
}

module.exports = {
    formatDate: formatDate,
    formatDateBefore: formatDateBefore,
    formatDateAfter: formatDateAfter,
    formatTime: formatTime,
    showSuccess: showSuccess,
    showLoading: showLoading,
    hideToast: hideToast,
    alertView: alertView,
    alertViewWithCancel: alertViewWithCancel,
    dateDifference: dateDifference
}
